#!/usr/bin/python3

import sys
import requests

# 硬编码Telegram Bot Token（替换为您自己的Token）
TELEGRAM_TOKEN = "8026467787:AAER2c_3SWEKDox8YyMOiiZDue2AELGEXfU"
BASE_URL = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/"

def send_telegram_alert(chat_id, subject, message):
    """发送告警到指定Telegram群组"""
    formatted_msg = f"*{subject}*\n\n{message}"  # Markdown格式
    
    payload = {
        "chat_id": chat_id,
        "text": formatted_msg,
        "parse_mode": "Markdown"
    }
    
    try:
        response = requests.post(BASE_URL + "sendMessage", json=payload)
        response.raise_for_status()
        print(f"Telegram消息已发送至群组 {chat_id}")
        return True
    except Exception as e:
        print(f"Telegram推送失败: {str(e)}")
        return False

if __name__ == "__main__":
    if len(sys.argv) != 4:
        print("Usage: zabbix-tg-alart.py <chat_id> <subject> <message>")
        sys.exit(1)
    
    _, sendto, subject, message = sys.argv
    send_telegram_alert(sendto, subject, message)